﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcExceptionManagementSample
{
    public class CustomExceptionAttribute : FilterAttribute, IExceptionFilter
    {
        #region IExceptionFilter Members

        public void OnException(ExceptionContext filterContext)
        {
            //if you uncomment next block the attribute will handle twice the same exception caught by the BaseController
            //if you override OnException in controller in which you use this custom attribute and uncomment the block below,
            //loging will never happen
            //if (filterContext.ExceptionHandled)
            //    return;

            //do logging on exception
            log4net.ILog log = log4net.LogManager.GetLogger(GetType());
            log.Error(filterContext.Exception.Message, filterContext.Exception);
            filterContext.ExceptionHandled = true;
        }

        #endregion
    }
}
